﻿using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace HangzhouChipProject
{
    public partial class IoCard : FrmWithOKCancel1
    {
        const short CORE = 1;  //板卡
        //指示返回值
        short sRtn;
        short sEcatSts;
        short axis;
        bool[] en = new bool[8];
        double vel, prfvel, prfpos, encvel, encpos;
        int pose;
        mc.TJogPrm jog;
        mc.TTrapPrm trapPrm;
        AxisConfig axisConfigcommon = new AxisConfig();
        AxisConfig axisConfigPose = new AxisConfig();
        public IoCard()
        {
            InitializeComponent();
            this.Title = "运动控制";
            this.IsShowCloseBtn = true;
            this.BackColor = Color.FromArgb(128, 128, 255);
            timer_ReadPose.Start();
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {

            // 关闭运动控制器
            timer_ReadPose.Stop();
            base.OnFormClosed(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //  初始化XML保存参数
            InterLayer.InitXmlDataParamer();
            this.Axis_Combox.SelectedIndex = 0;


        }
        private void PrintfLog(string slog)
        {
            jogPritf_Tb.AppendText(string.Format("消息提示: {0},{1},{2}", DateTime.Now.ToString(), slog, Environment.NewLine));
        }
        private void ucBtnExt_GTN_Reset_BtnClick(object sender, EventArgs e)
        {
            if (MotorsControl.MotionReset(CORE) == 0)
            {
                PrintfLog("GTN_Reset");

            }

        }
        private void ucBtnExt_initCard_BtnClick(object sender, EventArgs e)
        {
            if (MotorsControl.MotorsInit(CORE) == 0)
            {
                PrintfLog("GTN_LoadConfig");
            }
        }

        private void ucArrow_Right_MouseDown(object sender, MouseEventArgs e)
        {
            axis = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
            vel = Convert.ToDouble(this.textBox_vel.Text);
            jog.acc = Convert.ToDouble(this.textBox_acc.Text);
            jog.dec = Convert.ToDouble(this.textBox_dec.Text);
            jog.smooth = Convert.ToDouble(this.textBox_Smooth.Text);
            MotorsControl.JogNegativeMove(CORE, axis, jog, vel);
        }

        private void ucArrow_Right_MouseUp(object sender, MouseEventArgs e)
        {
            MotorsControl.MotorStop(CORE, axis);
        }

        private void timer_ReadPose_Tick(object sender, EventArgs e)
        {
            axis = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
            MotorsControl.MonitorReadPose(CORE, short.Parse(axis.ToString()), out prfpos, out prfvel, out encpos, out encvel);
            this.prfvel_tb.Text = prfvel.ToString();
            this.prfpos_tb.Text = prfpos.ToString();
            this.encvel_tb.Text = encvel.ToString();
            this.encpos_tb.Text = encpos.ToString();
            mc.TPosCompareStatus posCompareStatus;
            mc.GTN_PosCompareStatus(1, 1, out posCompareStatus);
            pluseNum_tb.Text = posCompareStatus.pulseCount.ToString();
        }

        private void ucBtnExt_StartRuning_BtnClick(object sender, EventArgs e)
        {
            axis = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
            vel = Convert.ToDouble(this.textBox_vel.Text);
            pose = Convert.ToInt32(this.pose_tb.Text);
            trapPrm.acc = Convert.ToDouble(this.textBox_acc.Text);
            trapPrm.dec = Convert.ToDouble(this.textBox_dec.Text);
            trapPrm.smoothTime = short.Parse(this.SmoothTime_tb.Text);
            MotorsControl.PointMove(CORE, axis, trapPrm, vel, pose);
            //for (int i = 1; i < 8; i++)
            //{
            //    mc.GTN_AxisOn(CORE, short.Parse(i.ToString()));
            //}

            // int flyStartpose5= 32410;
            // int flyStartpose6= 9084;
            // Parallel.Invoke(() => 
            // { 
            //     s1 = MotorsControl.PointMove(CORE, 1, trapPrm, 20, -287549);
            // },
            // () => { 
            //     s2 = MotorsControl.PointMove(CORE, 2, trapPrm, 20, 327293);
            // });
            // Parallel.Invoke(() =>
            // {
            //     MotorsControl.PointMove(CORE, 5, trapPrm, 20, flyStartpose5);
            // },
            //() => {
            //    S6 = MotorsControl.PointMove(CORE, 6, trapPrm, 20, flyStartpose6);
            //});
            // int flyStartpose5offset = 32000;
            // int flyStartpose6offset = -4000;
            // if ((S6 & 0x10) == 0)
            // {
            //     MotorsControl.Flymove(CORE, new short[] { 5, 6 }, 7, trapPrm, new double[] { 20, 10 }, new int[] { flyStartpose5, flyStartpose6,flyStartpose5offset, flyStartpose6offset });
            // }
            // s1 = MotorsControl.PointMove(CORE, 1, trapPrm, 20, 375200);
            // if ((s1 & 0x10) == 0)
            // {

            //     MotorsControl.PickupProduct(CORE, new short[] { 3, 4 ,8}, trapPrm, new double[] { 20, 10,10 }, new int[] { 131187, -36250 ,20000});
            //    // MotorsControl.ioSignalEXOMonitoring(CORE, 12, 9, 1);
            // }
            // MotorsControl.LayDownProduct(CORE, new short[] { 3, 4 ,8}, trapPrm, new double[] { 20, 10,10 }, new int[] { -11914, 30645, 20000 });
        }

        private void ucBtnExt_StopRuning_BtnClick(object sender, EventArgs e)
        {
            axis = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
            MotorsControl.MotorsAxisOnoff(CORE, axis);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MotorsControl.stopCompare();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MotorsControl.setCompareMode(new short[] { 3 }, new short[] { 1 }, 2, 1, 0, 1, 1, 0, 100);
            MotorsControl.setCompareData_Pso(500);
            MotorsControl.startCompare();
        }

        private void ucArrow_Right_Load(object sender, EventArgs e)
        {

        }

        private void ucArrow_Right_Load_1(object sender, EventArgs e)
        {

        }

        private void ucBtnExt_Savexml_BtnClick(object sender, EventArgs e)
        {
            try
            {

                axisConfigcommon.Id = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
                axisConfigcommon.Vel = Convert.ToDouble(this.textBox_vel.Text);
                axisConfigcommon.Acc = Convert.ToDouble(this.textBox_acc.Text);
                axisConfigcommon.Dec = Convert.ToDouble(this.textBox_dec.Text);
                axisConfigcommon.Smooth = short.Parse(this.SmoothTime_tb.Text);
                switch (axisConfigcommon.Id)
                {
                    case 5:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_1poseoffly.Text);
                        axisConfigcommon.Posetwo = Convert.ToInt32(this.textBox_1poseofPick.Text);
                        break;
                    case 6:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_2Ngpose.Text);
                        break;
                    case 7:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_3Pickpose.Text);
                        axisConfigcommon.Posetwo = Convert.ToInt32(this.textBox_3Pushpose.Text);
                        break;
                    case 8:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_4pickpose.Text);
                        axisConfigcommon.Posetwo = Convert.ToInt32(this.textBox_4Pushpose.Text);
                        break;
                    case 1:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_5startpose.Text);
                        axisConfigcommon.Posetwo = Convert.ToInt32(this.textBox_5endpose.Text);
                        break;
                    case 2:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_6startpose.Text);
                        axisConfigcommon.Posetwo = Convert.ToInt32(this.textBox_6endpose.Text);
                        break;
                    case 3:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_7startpose.Text);
                        axisConfigcommon.Posetwo = Convert.ToInt32(this.textBox_7endpose.Text);
                        break;
                    case 4:
                        axisConfigcommon.Poseone = Convert.ToInt32(this.textBox_8pickpose.Text);
                        break;
                }
                InterLayer.EditPointsMoveAxisdata(axisConfigcommon.Id, axisConfigcommon);
                PrintfLog("参数保存成功 " + axis);
            }
            catch (Exception ex)
            {

                MessageBox.Show("参数保存异常");
            }

        }
        /// <summary>
        /// 加载保存的初始值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Axis_Combox_SelectedIndexChanged(object sender, EventArgs e)
        {
            InterLayer.MyAxisData myAxisData = new InterLayer.MyAxisData();
            int axisid = Convert.ToInt32(this.Axis_Combox.Text);
            InterLayer.GetNodeInnerText(axisid, ref myAxisData);
            this.textBox_vel.Text = myAxisData.Vel;
            this.textBox_acc.Text = myAxisData.Acc;
            this.textBox_dec.Text = myAxisData.Dec;
            this.SmoothTime_tb.Text = myAxisData.Smooth;
            for (int i = 1; i < 9; i++)
            {
                InterLayer.GetNodeInnerText(i, ref myAxisData);
                switch (i)
                {
                    case 5:
                        this.textBox_1poseoffly.Text = myAxisData.Poseone.ToString();
                        this.textBox_1poseofPick.Text = myAxisData.Posetwo.ToString();
                        break;
                    case 6:
                        this.textBox_2Ngpose.Text = myAxisData.Poseone.ToString();
                        break;
                    case 7:
                        this.textBox_3Pickpose.Text = myAxisData.Poseone.ToString();
                        this.textBox_3Pushpose.Text = myAxisData.Posetwo.ToString();
                        break;
                    case 8:
                        this.textBox_4pickpose.Text = myAxisData.Poseone.ToString();
                        this.textBox_4Pushpose.Text = myAxisData.Posetwo.ToString();
                        break;
                    case 1:
                        this.textBox_5startpose.Text = myAxisData.Poseone.ToString();
                        this.textBox_5endpose.Text = myAxisData.Posetwo.ToString();
                        break;
                    case 2:
                        this.textBox_6startpose.Text = myAxisData.Poseone.ToString();
                        this.textBox_6endpose.Text = myAxisData.Posetwo.ToString();
                        break;
                    case 3:
                        this.textBox_7startpose.Text = myAxisData.Poseone.ToString();
                        this.textBox_7endpose.Text = myAxisData.Posetwo.ToString();
                        break;
                    case 4:
                        this.textBox_8pickpose.Text = myAxisData.Poseone.ToString();
                        break;
                }
            }
        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void textBox_1poseoffly_TextChanged(object sender, EventArgs e)
        {

        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
          
        }

        private void ucBtnExt_AxisOn_BtnClick(object sender, EventArgs e)
        {
            axis = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
            if (MotorsControl.MotorsAxisOnoff(CORE, axis))
            {
                // ucBtnExt_AxisOn.BtnText = "使能OFF";
                PrintfLog("GTN_AxisOn " + axis);

            }
            else
            {
                // ucBtnExt_AxisOn.BtnText = "使能ON";
                PrintfLog("GTN_AxisOFF " + axis);
            }

        }

        private void ucBtnExt_PoseClear_BtnClick(object sender, EventArgs e)
        {
            MotorsControl.MotorsPoseClear(CORE, 1, 8);
        }
        private void ucArrow_Left_MouseDown(object sender, MouseEventArgs e)
        {
            axis = Convert.ToInt16(this.Axis_Combox.SelectedIndex + 1);
            vel = Convert.ToDouble(this.textBox_vel.Text);
            jog.acc = Convert.ToDouble(this.textBox_acc.Text);
            jog.dec = Convert.ToDouble(this.textBox_dec.Text);
            jog.smooth = Convert.ToDouble(this.textBox_Smooth.Text);
            MotorsControl.JogPositiveMove(CORE, axis, jog, vel);
        }

        private void ucArrow_Left_MouseUp(object sender, MouseEventArgs e)
        {
            MotorsControl.MotorStop(CORE, axis);
        }

    }
}
